﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    public class CourseBS
    {
        #region 公有方法
        /// <summary>
        /// 获取单个课程
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public Course GetCourseInfo(string course_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Course model = new Course();
                //var ret = db.course.Where(x => x.course_id == course_id);
                var course = db.course.FirstOrDefault(x => x.course_id == course_id);
                model.course_id = course.course_id;
                model.course_name = course.course_name;
                model.course_points = course.course_points;
                model.course_stutype = course.course_stutype;
                model.course_type = course.course_type;
                model.course_hours_x = course.course_hours_x;
                model.course_hours_y = course.course_hours_y;
                return model;
            }
        }

        /// <summary>
        /// 获取所有课程列表
        /// </summary>
        /// <returns></returns>
        static public List<Course> GetCourseList()
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var ret = db.course;
                List<Course> list = new List<Course>();
                foreach (var course in ret)
                {
                    Course model = new Course();
                    model.course_id = course.course_id;
                    model.course_name = course.course_name;
                    model.course_points = course.course_points;
                    model.course_stutype = course.course_stutype;
                    model.course_type = course.course_type;
                    model.course_hours_x = course.course_hours_x;
                    model.course_hours_y = course.course_hours_y;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 获取教师未带课列表
        /// </summary>
        /// <param name="teacher_id"></param>
        /// <returns></returns>
        static public List<Course> GetUnCourseList(string teacher_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                var ret = db.course.Where(x => x.teacher.All(t => t.teacher_id != teacher_id));
                List<Course> list = new List<Course>();
                foreach (var course in ret)
                {
                    Course model = new Course();
                    model.course_id = course.course_id;
                    model.course_name = course.course_name;
                    model.course_points = course.course_points;
                    model.course_stutype = course.course_stutype;
                    model.course_type = course.course_type;
                    model.course_hours_x = course.course_hours_x;
                    model.course_hours_y = course.course_hours_y;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 获取教师带课列表
        /// </summary>
        /// <param name="teacher_id"></param>
        /// <returns></returns>
        static public List<Course> GetCourseList(string teacher_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                //var ret = db.course.Where(x => x.teacher.Where(t => t.teacher_id == teacher_id).FirstOrDefault().teacher_id == teacher_id);
                var ret = db.course.Where(x => x.teacher.Any(t => t.teacher_id == teacher_id));

                List<Course> list = new List<Course>();
                foreach (var course in ret)
                {
                    Course model = new Course();
                    model.course_id = course.course_id;
                    model.course_name = course.course_name;
                    model.course_points = course.course_points;
                    model.course_stutype = course.course_stutype;
                    model.course_type = course.course_type;
                    model.course_hours_x = course.course_hours_x;
                    model.course_hours_y = course.course_hours_y;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 获取指定课程的教学大纲列表
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public List<Course_syllabus> GetCourse_syllabusList(string course_id)
        {
            return Course_syllabusBS.GetCourse_syllabusList(course_id);
        }

        /// <summary>
        /// 添加课程
        /// </summary>
        /// <param name="course"></param>
        /// <returns>成功返回true</returns>
        static public bool AddCourse(Course model)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    db.course.AddObject(new course()
                    {
                        course_id = model.course_id,
                        course_name = model.course_name,
                        course_points = model.course_points,
                        course_stutype = model.course_stutype,
                        course_type = model.course_type,
                        course_hours_x = model.course_hours_x,
                        course_hours_y = model.course_hours_y,
                    });
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 为课程指定受课班级
        /// </summary>
        /// <param name="teacher_id">课程ID</param>
        /// <param name="course_id">班级ID</param>
        /// <returns>是否成功</returns>
        static public bool AddClass(string course_id, string class_id)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    var course = db.course.FirstOrDefault(x => x.course_id == course_id);
                    if (course.@class == null) //外键表，要自己new不？
                    {
                        course.@class = new System.Data.Objects.DataClasses.EntityCollection<@class>();
                    }
                    var target = db.@class.FirstOrDefault(c => c.class_id == class_id);
                    course.@class.Add(target);
                    db.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 为课程指定受课专业
        /// </summary>
        /// <param name="teacher_id">课程ID</param>
        /// <param name="course_id">班级ID</param>
        /// <returns>是否成功</returns>
        static public bool AddPro_emphasis(string course_id, string pro_emphasis_id)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    var course = db.course.FirstOrDefault(x => x.course_id == course_id);
                    if (course.@class == null) //外键表，要自己new不？
                    {
                        course.@class = new System.Data.Objects.DataClasses.EntityCollection<@class>();
                    }
                    var target = db.pro_emphasis.FirstOrDefault(c => c.pro_emphasis_id == pro_emphasis_id);
                    course.pro_emphasis.Add(target);
                    db.SaveChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion
    }
}
